
// Definition for a binary tree node.
struct TreeNode
{
    int val;
    TreeNode *left;
    TreeNode *right;
    TreeNode() : val(0), left(nullptr), right(nullptr) {}
    TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
    TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
};

class Solution
{
public:
//递归判断每个分支需不需要剪裁 + 递归检测(这个分支是否需要剪裁)
    bool test(TreeNode *root)
    {
        if (!root || root->val == 0 && test(root->left) && test(root->right))
            return true;
        return false;
    }
    void del(TreeNode *root)
    {
        if (!root)
            return;
        if (test(root->left))
            root->left = nullptr;
        if (test(root->right))
            root->right = nullptr;
        del(root->left), del(root->right);
    }
    TreeNode *pruneTree(TreeNode *root)
    {
        if (test(root))
            return nullptr;
        del(root);
        return root;
    }
};